local Msg = require("core.Msg")
local FriendDefine = require("friend.FriendDefine")
local FriendLogic = require("friend.FriendLogic")
local RoleDBLogic = require("role.RoleDBLogic")
local Human = require("core.Human")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local RoleFFI = require("role.RoleFFI")
local LibaoConfig = require("excel.Friend").libao
local Grid = require("bag.Grid")
local MailLogicFW = require("mail.MailLogicFW")

function CG_FRIEND_QUERY(human,msg)
	local type = msg.type
	if type == FriendDefine.FRIEND then
		FriendLogic.sendFriendList(human)	
	elseif type == FriendDefine.BLACK then
		FriendLogic.sendBlackList(human)
	elseif type == FriendDefine.ENEMY then
		FriendLogic.sendEnemyList(human)
	end
end

function CG_FRIEND_AUTO_ADD(human)
  FriendLogic.changeAutoAdd(human)
end

function CG_FRIEND_ADD(human,msg)
	local type = msg.type
	local targetUuid = msg.targetUuid
	if type == FriendDefine.ADD_FRIEND then
		FriendLogic.GC_FRIEND_ADD_NOTIFY(human._id,targetUuid)
	elseif type == FriendDefine.ADD_BLACK then
		FriendLogic.addBlack(human,targetUuid)
	end
end

function CG_FRIEND_ADD_NOTIFY_RET(human,msg)
	local result = msg.result
	local invitorUuid = msg.invitorUuid
	
	FriendLogic.onAddFriendRequestRet(invitorUuid,human._id)
	if result == FriendDefine.ADD_FRIEND_AGREE then
		FriendLogic.addFriend(invitorUuid,human._id)
	elseif result == FriendDefine.ADD_FRIEND_REFUSE then
		local target = Human.onlineUuid[invitorUuid]
		if target then
			Broadcast.sendBroadcast(target,Lang.DOWN,human.name .. Lang.FRIEND_TARGET_REFUSE_INVITE)
		end
	end
	
end

function CG_FRIEND_DEL(human,msg)
	local type = msg.type
	local targetUuid = msg.targetUuid	
	if type == FriendDefine.DEL_FRIEND then
		FriendLogic.delFriend(human._id,targetUuid)
	elseif type == FriendDefine.DEL_BLACK then
		FriendLogic.delBlack(human._id,targetUuid)
	elseif type == FriendDefine.DEL_ENEMY then
		FriendLogic.delEnemy(human,targetUuid)	
	end
end

function CG_FRIEND_FIND(human,msg)
	local data = RoleDBLogic.getDbByName(msg.name)
	if data then
		FriendLogic.GC_FRIEND_FIND(human,data)
	else
		Broadcast.sendBroadcast(human,Lang.MOUSE,Lang.FRIEND_FIND_NOT_EXIST)
	end
end

function CG_FRIEND_CHAT(human,msg)
	if human._id == msg.recUuid then
		return
	end
	--FriendLogic.addRecent(human,msg.recUuid)
	FriendLogic.CG_FRIEND_CHAT(human,msg)
end

local f = {
	name = 1,
	head = 1,
	sign = 1,
	lv = 1,
	job = 1,
	sex = 1,
	vipLv = 1,
	familyUuid = 1,	
}
function CG_FRIEND_ASK_CHAT(human,msg)
	local targetUuid = msg.targetUuid
	if human._id == targetUuid then
		return
	end
	local targetDB = RoleDBLogic.getDb(targetUuid, f)
	if not targetDB then
		return
	end
	local msgRet = Msg.gc.GC_FRIEND_ASK_CHAT
	msgRet.targetUuid = targetUuid
	msgRet.targetName = RoleFFI.ffi_string(targetDB.name)
	local target = Human.onlineUuid[targetUuid]
	msgRet.id = target and target.id or 0
	msgRet.head = targetDB.head or 0
	msgRet.sign = ""
	msgRet.lv = targetDB.lv 
	msgRet.sex = targetDB.sex
	msgRet.vipLv = 0--targetDB.vipLv or 0
	msgRet.familyName = ""
	Msg.send(msgRet,human.fd)
end

function CG_FRIEND_ACTIVITY_COMMENT(human,msg)
	FriendLogic.CG_FRIEND_ACTIVITY_COMMENT(human,msg.activityUuid,msg.content)
end

function CG_FRIEND_ACTIVITY_DEL(human,msg)
	FriendLogic.CG_FRIEND_ACTIVITY_DEL(human,msg.activityUuid)
end

function CG_FRIEND_QINMI_CONF_QUERY()
	--FriendLogic.CG_FRIEND_QINMI_CONF_QUERY(human)
end

function LW_SEND_KILLER(human, msg)
  FriendLogic.addEnemy(human, msg.killUuid)
end
--给好友送礼包
function CG_FRIEND_SEND_LIBAO(human, msg)
--  if msg.uuid == "" then
--    return
--  end
--  if human.db.sendFriendLibaoLen == 1 then
--    return
--  end
--  for i=1, human.db.sendFriendLibaoLen do
--    if msg.uuid == human.db.sendFriendLibao[i].uuid then
--      return
--    end
--  end
--  local obj = Human.onlineUuid[msg.uuid]
--  if obj then
--    obj.db.getFriendLibaoTimes = obj.db.getFriendLibaoTimes + 1
--  else
--    local db = RoleDBLogic.getDbByUuid(msg.uuid)
--    if not db then
--      return
--    end
--    RoleDBLogic.saveRoleSset({_id = msg.uuid, getFriendLibaoTimes = db.getFriendLibaoTimes + 1})
--  end
--  local nGridList = {}
--  for i=1, #LibaoConfig[1].libao do
--    nGridList[1] = Grid.create(nil, LibaoConfig[1].libao[i][1], LibaoConfig[1].libao[i][2], 1)
--  end
--  human.db.sendFriendLibao[human.db.sendFriendLibaoLen].uuid = msg.uuid 
--  human.db.sendFriendLibaoLen = human.db.sendFriendLibaoLen + 1
--  local str = string.format("福星高照，您的好友%s给您赠送了大礼包，请查收", human.name)
--  MailLogicFW.addSystemMessage(msg.uuid, "好友赠礼", str, nGridList)
--  sendLibaoInfo(human)
end
--发送礼包内容
function sendLibaoInfo(human)
--  local sendMsg = Msg.gc.GC_FRIEND_LIBAO_INFO
--  local cache = {}
--  sendMsg.libao[0] = #LibaoConfig[1].libao 
--  for i=1, #LibaoConfig[1].libao do
--    Grid.create(cache, LibaoConfig[1].libao[i][1], LibaoConfig[1].libao[i][2])
--    Grid.makeItem(cache, sendMsg.libao[i], 0, human)
--  end
--  sendMsg.uuid[0] = human.db.sendFriendLibaoLen
--  for i=1, human.db.sendFriendLibaoLen do
--    sendMsg.uuid[i] = human.db.sendFriendLibao[i].uuid
--  end
--  Msg.send(sendMsg, human.fd)
end